<?php

namespace app\common\model\mysql;

class Category extends BaseModel {

    /**
     * @param string $field
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getNormalCategorys($field = '*') {
        $where = [
            'status' => config('status.mysql.table_normal'),
        ];
        $order = [
            'listorder' => 'desc',
            'id'        => 'desc'
        ];

        $result = $this->where($where)
            ->field($field)
            ->order($order)
            ->select();
        //halt($this->getLastSql());
        return $result;
    }

    /**
     * 分页列表
     * @param $where
     * @param int $num
     * @param string $field
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getLists($where, $num = 10, $field = '*') {
        $order = [
            'listorder' => 'desc',
            'id'        => 'desc'
        ];

        $result = $this->where('status', '<>', config('status.mysql.table_delete'))
            ->where($where)
            ->field($field)
            ->order($order)
            ->paginate($num);
        return $result;
    }

    public function getCategoryById($id, $field = '*') {
        $where = [
            'id'     => $id,
            'status' => config('status.mysql.table_normal')
        ];
        $result = $this->where($where)
            ->field($field)
            ->find();
        return $result->toArray();
    }

    public function getCategoryByName($name) {
        $result = $this->where('name', $name)
            ->select()->toArray();
        return $result;
    }

    public function getChildCategoryByPids($condition, $field = ['id', 'pid', 'name']) {
        $where[] = ['pid', 'in', $condition['pid']];
        $where[] = ['status', '<>', config('status.mysql.table_delete')];

        $res = $this->where($where)
            ->field($field)
            ->select();
        return $res->toArray();
    }

    public function getLimitCategoryByPids($condition, $field = ['id', 'pid', 'name'], $limit = 3) {
        $where[] = ['pid', 'in', $condition['pid']];
        $where[] = ['status', '<>', config('status.mysql.table_delete')];

        $order = [
            'listorder' => 'desc',
            'id'        => 'desc'
        ];

        $res = $this->where($where)
            ->field($field)
            ->order($order)
            ->limit($limit)
            ->select();
        return $res->toArray();
    }

    /**
     * getChildCountInPids
     * @param $condition
     * @return mixed
     */
    public function getChildCountInPids($condition) {
        $where[] = ['pid', 'in', $condition['pid']];
        $where[] = ['status', '<>', config('status.mysql.table_delete')];

        $res = $this->where($where)
            ->field(['pid', 'count(*) as count'])
            ->group('pid')
            ->select();
        return $res;
    }

    public function getNormalByPid($pid = 0, $field = '*') {
        $where = [
            'pid'    => $pid,
            'status' => config('status.mysql.table_normal'),
        ];

        $order = [
            'listorder' => 'desc',
            'id'        => 'desc'
        ];

        $res = $this->where($where)
            ->field($field)
            ->order($order)
            ->select();
        return $res;
    }

    public function getNormalByPids($pids, $field = '*') {
        $where[] = ['pid', 'in', $pids];
        $where[] = ['status', '=', config('status.mysql.table_normal')];

        $order = [
            'listorder' => 'desc',
            'id'        => 'desc'
        ];

        $res = $this->where($where)
            ->field($field)
            ->order($order)
            ->select();
        return $res;
    }
}